﻿using STHPMS1.BLL.B_Core;
using STHPMS1.ICore.I_BLL.Bus;
using STHPMS1.ICore.I_Entity.Info;
using STHPMS1.ICore.I_Entity.Stmpms1;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using STHPMS1.ICore.I_DAL;
using STHPMS1.Common.C_Entity.Stmpms1;
using System.Linq;
using System.Data;

namespace STHPMS1.BLL.B_Bus
{
    public class B_RoadnetBus : BllBase, IB_RoadnetBill
    {
        public T GetSingle<T>(string id) where T : class, IB_Roadnet
        {
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            return dao.Single<T>(x => x.Roadnet_Name == id);
        }

        public void Save<T>(T entity)
            where T : class ,IB_Roadnet
        {
            if (this.Scene.Dal.Sthpms1.Update(entity) == 0)
            {
                this.Scene.Dal.Sthpms1.Insert(entity);
            }
        }

        public void Remove(IB_Roadnet entity)
        {
            this.Scene.Dal.Sthpms1.Delete(entity);
        }

        public IEnumerable<T> Get<T>(Expression<Func<T, bool>> predicate, IPagingInfo info)
            where T : class, IB_Roadnet
        {
            return this.Scene.Dal.Sthpms1.Select<T>(predicate, info);
        }


        /// <summary>
        /// 定义路网的条件转换为SQL查询条件
        /// </summary>
        /// <param name="roadnetname"></param>
        /// <returns></returns>
        public string GetSearchSQL(string roadnetname)
        {
            string returnvalues = "";
            B_Roadnet info = this.Scene.Dal.Sthpms1.Single<B_Roadnet>(p => p.Roadnet_Name == roadnetname);
            if (!string.IsNullOrEmpty(info.Admdiv))  //行政区域
            {
                if (info.Admdiv.Substring(2) == "0000") //省级
                {
                    //info.Admdiv.Substring(0,2)
                    returnvalues += " and B_INVNTORY.ADMDIV like '%" + info.Admdiv.Substring(0, 2) + "%' ";
                }
                else if (info.Admdiv.Substring(4) == "00") //市级
                {
                    //info.Admdiv.Substring(0,4)
                    returnvalues += " and B_INVNTORY.ADMDIV like '%" + info.Admdiv.Substring(0, 4) + "%' ";
                }
                else  //区域
                {
                    returnvalues += " and B_INVNTORY.ADMDIV='"+info.Admdiv+"'";
                }
            }
            if (!string.IsNullOrEmpty(info.Admgrd)) //行政等级
            {
                returnvalues += " and B_INVNTORY.ADMGRD in("+info.Admgrd+")";
            }
            if (!string.IsNullOrEmpty(info.Rdclass_Dictid)) //技术等级
            {
                returnvalues += " and B_INVNTORY.RDCLASS_DICTID in(" + info.Rdclass_Dictid + ")";
            }
            if (!string.IsNullOrEmpty(info.Pvmttype)) //路面类型
            {
                returnvalues += " and B_INVNTORY.PVMTTYPE_DICTID in(" + info.Pvmttype + ")";
            }
            if (!string.IsNullOrEmpty(info.Manaut)) //管养单位
            {
                returnvalues += " and B_INVNTORY.MANAUT like '%"+info.Manaut+"%'";
            }
            return returnvalues;
        }

        //public Dictionary<string, string> Dicts()
        //{
        //    ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
        //    return dao.Dictionary<string, string>(dao.From<B_Roadway>().Select(x => new { x.Rdwayid, x.Name }));
        //}


        
    }
}
